package com.pipayshop.controller;

import com.pipayshop.entity.vo.*;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.HotelShopCommodityLiveInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@Api(value = "图片相关接口", tags = "图片相关接口")
@RestController
@RequestMapping("/pipayshopapi/store-commodity-live-info")
@Slf4j
public class HotelImageController {

    @Resource
    private HotelShopCommodityLiveInfoService shopCommodityLiveInfoService;

    @PostMapping("roomTopImageUp")
    @ApiOperation("酒店头像图片上传")
    public ResponseVO<String> roomTopImageUp(MultipartFile multipartFile){

        return handleImageUpload(() -> shopCommodityLiveInfoService.roomTopImageUp(multipartFile),
                "hotel_avatar_upload_failed");
    }

    @PostMapping("roomImageUp")
    @ApiOperation("酒店轮播图上传")
    public ResponseVO<String> roomImageUp(MultipartFile file){
        return handleImageUpload(() -> shopCommodityLiveInfoService.roomImageUp(file),
                "hotel_carousel_image_upload_failed");
    }

    // 通用图片上传处理逻辑，减少代码重复
    private ResponseVO<String> handleImageUpload(ImageUploadFunction uploadFunction, String errorMessage) {
        try {
            String imageId = uploadFunction.upload();
            if ("图片未通过审核".equals(imageId)){
                throw new BusinessException("image_not_approved");
            }
            return ResponseVO.getSuccessResponseVo(imageId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException(errorMessage);
        }
    }

    @FunctionalInterface
    interface ImageUploadFunction {
        String upload() throws Exception;
    }

}
